import { Notify } from 'quasar'
import Vue from 'vue'
import { i18n } from 'src/boot/i18n';
// import { i18n } from 'src/boot/i18n'
let err = ''
let errList = []
let warn = ''
let warnList = []
let dialogInfo = ''

const error = function (messgae) {
  // 判断评论，避免相同
  if (err !== messgae && typeof messgae !== 'undefined') {
    err = messgae
    errList.push(
      Notify.create({
        color: 'red',
        textColor: 'black',
        position: 'top',
        timeout: 4000,
        message: messgae,
        onDismiss: function () {
          // 做异常弹出的节流
          errList.splice(0, 1) // 截掉当前
          if (errList.length < 1) { // 如果还是小与1
            err = ''
          }
        }
      })
    )
  }
};

const success = function (messgae) {
  Notify.create({
    color: 'green',
    textColor: 'black',
    position: 'top',
    timeout: 1500,
    message: messgae
  });
};

const warning = function (messgae, html) {
  if (warn !== messgae && typeof messgae !== 'undefined') {
    warn = messgae
    warnList.push(
      Notify.create({
        color: 'yellow',
        textColor: 'black',
        position: 'top',
        html: html,
        timeout: 4 * 1000,
        message: messgae,
        // closeBtn: '关闭',
        // closeBtn: i18n.t('notify.close')
        // actions: [{ icon: 'close', color: 'black' }],
        onDismiss: function () {
          warnList.splice(0, 1) // 截掉当前
          if (warnList.length < 1) { // 如果还是小与1
            warn = ''
          }
        }
      })
    )
  }
};

const dialog = function (dialogMessage) {
  if (dialogMessage !== dialogInfo && typeof dialogMessage !== 'undefined') {
    // console.info(dialogMessage)
    // 数据中有换行符就不进行换行操作
    dialogMessage = dialogMessage.replace(/(\r\n|\n|\r)/gm, '<br/>')
    dialogInfo = dialogMessage
    showDialog(dialogInfo)
  }
  function showDialog (dialogMessage) {
    Vue.prototype.$q.dialog({
      title: i18n.t('notice'),
      'no-esc-dismiss': true,
      'no-backdrop-dismiss': true,
      'no-route-dismiss': true,
      html: true,
      message: dialogMessage,
      ok: { noCaps: true, label: i18n.t('confirm'), flat: true }
    }).onOk(() => {
    }).onCancel(() => {
    }).onDismiss(() => {
      dialogInfo = ''
    })
  }
}

export default {
  error: error,
  warning: warning,
  success: success,
  dialog: dialog
}
